<?php
/**
* @file Usercheckm.php
* @synopsis  用户签到
* @author change, <change@medical-ligher.com>
* @version 1.0
* @date 2015年7月28日18:16:52
*/


class Usercheckm extends CI_Model
{
	function __construct()
	{
		$this->load->model('jfpointsm');
	}

	public function insert_log($data)
	{
        $this->load->model('newjfpointsm');

		$result = $this->db->insert("ml_user_check_log",$data);
		if($result)
		{
			//更新缓存缓存
			$mc = $this->cache->memcached;
	    	$key = 'userchecklog_' . $data['uid'];

	    	if(!$this->cache->memcached->is_supported())
	    	{
	    		$result = array("code" => -3,'tips' => "缓存未开启");
	    	}
	    	$mc->save($key,$data,$ttl=0);

	    	//签到送积分
	    	//$this->jfpointsm->user_check_jfpoint($data['uid']);
            $result = $this->newjfpointsm->add_jfpoints_for_one_user($data['uid'],DAILY_LOGIN,$data['uid'],DAILY_LOGIN);
	    	//连续十天以上送5分
	    	if($data['continue_days'] % 10 == 0 && $data['continue_days'] > 0)
	    	{
                $result = $this->newjfpointsm->add_jfpoints_for_one_user($data['uid'],DAILY_LOGIN_TO_TEN,$data['uid'],DAILY_LOGIN_TO_TEN);
	    	}
		}
		else
		{
			$result = array("code" => -2,'tips' => "签到失败");
		}
		return $result;
	}

	//获取用户签到记录
	public function get_insert_log($user_id)
	{
		$userchecklog = array();

		$mc = $this->cache->memcached;
    	$key = 'userchecklog_' . $user_id;

    	if(!$this->cache->memcached->is_supported())
    	{
    		$result = array("code" => -3,'tips' => "缓存未开启");
    	}

		$data_content = $mc->get($key);

		//有缓存 并且是当天的缓存 才不查询数据库
		$today = date("Y-m-d",time());
		if($data_content && $data_content['check_day'] == $today)
		{
			$userchecklog = $data_content;
		}
		else
		{
			$userchecklog = $this->db->select()->where('uid', $user_id)->order_by("id", "desc")->limit(1, 0)->get('ml_user_check_log')->result_array();
			$userchecklog = $userchecklog[0];
			$mc->save($key,$userchecklog,$ttl=0);
		}
		return $userchecklog;

	}

	//更新最后登录时间
	public function update_login_time($uid)
	{
        $this->db->set('last_login_time',time())->where('id',$uid)->update('ml_fellow');
	}

	//获取是否签到过
	public function get_is_first_check($user_id)
	{
		$this->redis = $this->cache->redis;
		$key = 'first_check_uid_::'.$user_id;
		$is_first_check = $this->redis->get($key);
		if(empty($is_first_check))
		{
			$userchecklog = $this->db->select()->where('uid', $user_id)->order_by("id", "desc")->limit(1, 0)->get('ml_user_check_log')->result_array();
			$is_first_check = $userchecklog ? 0 : 1;
			$this->redis->save($key,$is_first_check);
		}
		return $is_first_check;

	}
}